package cn.itcast.web.base.factory;



import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;


@Controller
@RequestMapping("base/factory/details")
public class FactoryController extends BaseController {

    @Reference
    private FactoryService factoryService;

    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize){

        FactoryExample factoryExample = new FactoryExample();

        factoryExample.setOrderByClause("create_time desc");

        PageInfo<Factory> pageInfo = factoryService.findByPage(factoryExample,pageNum,pageSize);
        //存储到请求域中
        request.setAttribute("pageInfo",pageInfo);
        return "base/factory/factory-list";
    }

    /*
    作用：进入工厂的添加
 */
    @RequestMapping("/toAdd")
    public String toAdd(){

         return "base/factory/factory-add";
    }


    /*
    作用： 保存添加与修改工厂
 */
    @RequestMapping("/edit")
    public String edit(Factory factory){

        //2.根据工厂的id判断是否是增加还是修改
        if(StringUtils.isEmpty(factory.getId())){
            //添加
            factoryService.save(factory);
        }else{
            //修改
            factoryService.update(factory);
        }
        //3. 返回工厂的列表页面
        return "redirect:/base/factory/details/list.do";
    }


    /*
       作用：进入修改页面
    */
    @RequestMapping("/toUpdate")
    public String toUpdate(String id){
        //1. 根据id查询当前厂家
        Factory factory = factoryService.findById(id);
        request.setAttribute("factory",factory);
        //2. 返回更新页面
        return "base/factory/factory-update";
    }

    /*
         作用：删除工厂
     */
    @RequestMapping("/delete")
    public String delete(String id){
        //1. 删除工厂
         factoryService.delete(id);
        return "redirect:/base/factory/details/list.do";
    }


    /*
      作用： 查看工厂
    */
    @RequestMapping("/toView")
    public String toView(String id){
        //根据id查找工厂
        Factory factory = factoryService.findById(id);
        //存储到请求域中
        request.setAttribute("factory",factory);
        return "base/factory/factory-view";
    }

}
